package com.addzero.autoddlstarter.context

import com.addzero.autoddlstarter.generator.consts.DbType


data class Settings(
    val dbType: String = DbType.POSTGRESQL,
    val idType: String = "bigint",
    val id: String = "id",
    val createBy: String = "create_by",
    val updateBy: String = "update_by",
    val createTime: String = "create_time",
    val updateTime: String = "update_time",
    val skipExistsFiles: String = "false", // 是否跳过已存在的文件
    val sharedSourceDir: String,
    val isomorphicPackageName: String,
    val isomorphicClassSuffix: String,
    val formPackageName: String,
    val apiClientPackageName: String = "com.addzero.kmp.generated.api"
)

val Settings.isomorphicShareOutPutDir: String
    get() {
        val settings = SettingContext.settings
        val replace = settings.isomorphicPackageName
            .replace(".", "/")
        return "${settings.sharedSourceDir}/$replace"
    }



val Settings.formShareOutPutDir: String
    get() {
        val settings = SettingContext.settings
        return "${settings.sharedSourceDir}/${settings.formPackageName
            .replace(".", "/")}"
    }

/**
 * API客户端输出目录
 */
val Settings.apiclientOutPutDir: String
    get() {
        val settings = SettingContext.settings
        return "${settings.sharedSourceDir}/${settings.apiClientPackageName
            .replace(".", "/")}"
    }




/**
 * 全局配置上下文
 */
object SettingContext {

    lateinit var settings: Settings

    val context: Settings
        get() = settings

    /**
     * 初始化配置
     * @param op gradle/ksp传入的options
     */
    fun initialize(op: Map<String, String>) {

        settings = Settings(
            dbType = op["dbType"] ?: DbType.POSTGRESQL,
            idType = op["idType"] ?: "bigint",
            id = op["id"] ?: "id",
            createBy = op["createBy"] ?: "create_by",
            updateBy = op["updateBy"] ?: "update_by",
            createTime = op["createTime"] ?: "create_time",
            updateTime = op["updateTime"] ?: "update_time",
            skipExistsFiles = op["skipExistsFiles"] ?: "false",

            sharedSourceDir = op["sharedSourceDir"] ?: "",
            isomorphicPackageName = op["isomorphicPackageName"] ?: "",
            isomorphicClassSuffix = op["isomorphicClassSuffix"] ?: "",

            formPackageName = op["formPackageName"] ?: "",
            apiClientPackageName = op["apiClientPackageName"] ?: "com.addzero.kmp.api"
        )

    }
}
